<template>
<el-dialog title="采购申请" :close-on-click-modal="false" :visible.sync="visible" width="90%">
    <el-table :data="dataList" border style="width: 100%;">
        <el-table-column header-align="center" align="center" label="封面" width="120">
            <template slot-scope="scope">
                <img v-if="scope.row.book.imgUrl" style="width: 100px; height: 100px" :src="scope.row.book.imgUrl" />
                <span v-else></span>
            </template>
        </el-table-column>
        <el-table-column prop="book.name" header-align="center" align="center" label="图书名称"></el-table-column>
        <el-table-column prop="book.type" header-align="center" align="center" label="分类" width="50"></el-table-column>
        <el-table-column prop="book.isbn" header-align="center" align="center" label="ISBN" width="125"></el-table-column>
        <el-table-column prop="book.author" header-align="center" align="center" label="作者"></el-table-column>
        <!-- <el-table-column prop="book.press" header-align="center" align="center" label="出版社"></el-table-column>
        <el-table-column header-align="center" align="center" label="出版时间" width="100">
            <template slot-scope="scope">{{scope.row.book.publishDate | formatYearMonth}}</template>
        </el-table-column>
      <el-table-column prop="book.language" header-align="center" align="center" label="语言" width="50"></el-table-column>-->
        <el-table-column prop="book.price" header-align="center" align="center" label="定价" width="80">
            <template slot-scope="scope">￥{{scope.row.book.price | formatPrice}}</template>
        </el-table-column>
        <el-table-column prop="applyDate" header-align="center" align="center" label="申请时间" width="100">
            <template slot-scope="scope">{{scope.row.applyDate | formatDate}}</template>
        </el-table-column>
        <el-table-column prop="applicant" header-align="center" align="center" label="申请人"></el-table-column>
        <el-table-column prop="purchaseId" header-align="center" align="center" label="申请编号" width="80"></el-table-column>
        <el-table-column prop="subNum" header-align="center" align="center" label="采购数量" width="80">
            <template slot-scope="scope">
                <el-input v-model="scope.row.subNum"></el-input>
            </template>
        </el-table-column>
        <el-table-column prop="readyNum" header-align="center" align="center" label="已采购数量" width="100"></el-table-column>
        <el-table-column prop="num" header-align="center" align="center" label="申请数量" width="80"></el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="showDiag = true">下订货单</el-button>
    </span>
    <el-dialog title="订货单" :visible.sync="showDiag" width="30%" :before-close="handleClose" append-to-body>
        <el-form :model="order" label-width="100px">
            <el-form-item label="供应商">
                <el-select v-model="order.supplierId" placeholder="请选择供应商">
                    <el-option v-for="item in supplierList" :key="item.id" :label="item.desc" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="备注" prop="note">
                <el-input v-model="order.note" placeholder="订单备注"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="showDiag = false">取 消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确 定</el-button>
        </span>
    </el-dialog>
</el-dialog>
</template>

<script>
export default {
    data() {
        return {
            supplierList: [{
                id: 1,
                desc: "北京当当网信息技术有限公司",
            }, ],
            statusList: [{
                id: 2,
                desc: "待采购",
            }, ],
            visible: false,
            dataListSelections: [],
            showDiag: false,
            dataList: [],
            order: {
                supplierId: null,
                note: "",
            },
        };
    },
    methods: {
        handleClose(done) {
            this.$confirm("确认关闭？")
                .then((_) => {
                    done();
                })
                .catch((_) => {});
        },
        init(data) {
            this.visible = true;
            this.dataList = data;
            this.dataList.forEach(function (item) {
                item.subNum = item.num - item.readyNum;
                if (item.book.img) {
                    item.book.imgUrl =
                        "http://localhost:8080/wenyuan/generator/tbook/img/" + item.book.id;
                }
            });
            console.log(this.dataList)
        },
        // 表单提交
        dataFormSubmit() {
            let bookList = [];
            this.dataList.forEach(function (item) {
                let book = {
                    purchaseId: item.purchaseId,
                    bookId: item.book.id,
                    num: item.subNum
                };
                bookList.push(book);
            });
            console.log(bookList)
            this.$http({
                url: this.$http.adornUrl(`/generator/torder/save`),
                method: "post",
                data: this.$http.adornData({
                    supplierId: this.order.supplierId,
                    note: this.order.note,
                    bookList: bookList,
                }),
            }).then(({
                data
            }) => {
                if (data && data.code === 0) {
                    this.$message({
                        message: "下单成功",
                        type: "success",
                        duration: 1500,
                        onClose: () => {
                            this.visible = false;
                            this.$emit("refreshDataList");
                        },
                    });
                    this.showDiag = false;
                    this.$emit("refreshDataList");
                } else {
                    this.$message.error(data.msg);
                }
            });
        },
    },
};
</script>
